package com.youhui.weixin.controller;

import com.youhui.weixin.domain.Goods;
import com.youhui.weixin.service.GoodsService;
import com.youhui.weixin.service.task.TransTask;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品Controller.
 * User: huang
 * Date: 17-10-20
 */
@Controller
@Log4j
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private TransTask transTask;


    @ResponseBody
    @RequestMapping("/trans/{size}/{page}")
    public String transLink(@PathVariable("size") int size, @PathVariable("page") int page ){
        transTask.transLinkData(page, size);
        return "Transform .... ";
    }
    // 短链接跳转到长链接
    @RequestMapping("/s/{uuid}")
    public String shortLink(@PathVariable("uuid") String uuid){
        return "redirect:"+goodsService.shortMapLong(uuid);
    }
    // 入参是一个js的数据 [id,id,id]
    @DeleteMapping("/delete/goods/{goodsList}")
    public boolean deleteGoods(@PathVariable("goodsList") String goodsList){
        return goodsService.deleteBatch(goodsList);
    }
    @ResponseBody
    @DeleteMapping("/delete/good/{goodsId}")
    public boolean  deleteGood(@PathVariable("goodsId") Long goodsId){
        log.info("删除商品："+goodsId);
        return goodsService.delete(goodsId);
    }
    /**
     * 分页获取热门商品.
     * @param pageNum 页码
     * @return 商品
     */
    @GetMapping("/hotgoods")
    @ResponseBody
    public Page<Goods> hotGoods(int pageNum, String all) {
        if (all != null) {
            pageNum += 1;
            return goodsService.historyGoods(pageNum);
        }
        return goodsService.getHotGoods(pageNum);

    }

    /**
     * 分页获取某类型的商品.
     * @param typeId 类型
     * @param pageNum 页码
     * @return
     */
    @GetMapping("/goodsbytype")
    @ResponseBody
    public Page<Goods> getGoodsByTypes(int typeId, int pageNum, String all) {
        if (all != null) {
            pageNum += 1;
            return goodsService.getHistoryTypeGoods(typeId, pageNum);
        }
        return goodsService.getTypeGoods(typeId, pageNum);
    }

    /**
     * 查询商品.
     * @param query 查询条件
     * @param pageNum 页码
     * @return
     */
    @GetMapping("/search")
    @ResponseBody
    public Page<Goods> searchGoods(String query, int pageNum, String all) {
        if (all != null) {
            pageNum += 1;
            return goodsService.historySearchGoods(query, pageNum);
        }
        return goodsService.searchGoods(query, pageNum);
    }

    /**
     * 搜索提示
     * @param query
     * @return
     */
    @GetMapping("/querytip")
    @ResponseBody
    public List<String> queryTip(String query) {
        return goodsService.queryGoods();
    }

}
